import log4js from 'log4js'
import config from '../config/log4js-frontend.json'
log4js.configure(config)

// 扩展Logger原型添加自定义级别方法
const Logger = log4js.getLogger().constructor
if (!Logger.prototype.verbose) {
  Logger.prototype.verbose = function(message, exception = null) {
    if (this.isLevelEnabled('verbose')) {
      if (exception) {
        this.log('verbose', message, exception);
      } else {
        this.log('verbose', message);
      }
    }
  }
}

if (!Logger.prototype.trace) {
  Logger.prototype.trace = function(message, exception = null) {
    if (this.isLevelEnabled('trace')) {
      if (exception) {
        this.log('trace', message, exception);
      } else {
        this.log('trace', message);
      }
    }
  }
}

/**
 * 创建日志实例工厂函数
 * @param {string} category - 日志分类名称
 * @returns {Logger}
 */
export default function getLogger(category) {
  return log4js.getLogger(category || 'default')
}